package com.tom.service.impl;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.tom.common.OrderComment;
import com.tom.common.OrderFoodUtil;
import com.tom.mapper.*;
import com.tom.pojo.*;
import com.tom.service.IRestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("restService")
public class RestServiceImpl implements IRestService {

    @Resource
    private RestaurantMapper restaurantMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private HeiZaiOrderMapper heiZaiOrderMapper;

    @Resource
    private OrderFoodMapper orderFoodMapper;

    @Resource
    private FoodMapper foodMapper;

    @Override
    public int register(Restaurant restaurant) {
        // 注册店铺,并改变登录用户类型
        int result1 = restaurantMapper.insertSelective(restaurant);
        int result2 = 0;
        while (result2<1) {
            result2 = userMapper.updateTypeByPrimaryKey(restaurant.getUserId());
        }
        return result1;
    }

    @Override
    public int setStatus(Integer id, Byte status) {
        Restaurant restaurant = new Restaurant();
        restaurant.setId(id);
        restaurant.setStatus(status);
        return restaurantMapper.updateByPrimaryKeySelective(restaurant);
    }

    @Override
    public Restaurant getMyRest(Integer userId) {
        return restaurantMapper.selectByUserId(userId);
    }

    @Override
    public List<Restaurant> listOpen() {
        return restaurantMapper.selectByStatus((byte) 2);
    }

    @Override
    public Restaurant getRestInfo(Integer id) {
        return restaurantMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateRest(Restaurant restaurant) {
        return restaurantMapper.updateByPrimaryKeySelective(restaurant);
    }

    @Override
    public List<OrderComment> getRestComments(Integer rid) {
        List<OrderComment> comments = new ArrayList<>();
        List<Comment> list = commentMapper.selectByRestId(rid);
        for (Comment comment :
                list) {
            // 根据comment的orderId查询订单餐品列表
            List<OrderFoodUtil> orderFoodUtils = new ArrayList<>();
            List<OrderFood> orderFoodList = orderFoodMapper.selectByOrderId(comment.getOrderId());
            for (OrderFood or :
                    orderFoodList) {
                Food food = foodMapper.selectByPrimaryKey(or.getFoodId());
                OrderFoodUtil foodUtil = new OrderFoodUtil();
                foodUtil.setId(food.getId());
                foodUtil.setTitle(food.getTitle());
                foodUtil.setPrice(food.getPrice());
                foodUtil.setCount(or.getNumber());

                orderFoodUtils.add(foodUtil);
            }
            // 单条评论
            OrderComment orderComment = new OrderComment();
            orderComment.setUserName(userMapper.selectByPrimaryKey(comment.getUserId()).getUsername());
            orderComment.setRate(comment.getRate());
            orderComment.setContent(comment.getContent());
            orderComment.setAddTime(comment.getAddTime());
            orderComment.setFoods(orderFoodUtils);
            comments.add(orderComment);
        }
        return comments;
    }
}
